/**
 * jims
 */
package com.jims.api.v1.controller.doctor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jims.master.common.data.Message;
import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.common.utils.StringUtils;
import com.baomidou.kisso.my.controller.BaseController;

import com.jims.master.modules.sync.entity.*;
import com.jims.master.modules.sync.service.ClinicMasterService;
import com.jims.master.modules.sync.service.ExamApplyService;
import com.jims.master.modules.sync.service.ExamItemService;
import com.jims.master.modules.sync.service.ExamTypeService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.service.SystemService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 检查申请Controller
 *
 * @author jict
 * @version 2017-03-14
 */
@RestController
    @RequestMapping(value = "/examApply")
public class ExamApplyController extends BaseController {

    @Autowired
    private ExamApplyService examApplyService;
    // @Autowired
    //private ExamTypeService examTypeService;//检查项目业务
    @Autowired
    private ExamItemService examItemService;
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private ExamTypeService examTypeService;//检查项目业务
    @Autowired
    private ClinicMasterService clinicMasterService;
    @Autowired
    private SystemService systemService;


    /**
     * 医生查看检查申请
     * @param clinicId
     * @Author ct
     * @return
     */
    @RequestMapping(value = {"V100/getExamApply"})
    public String getExamApply(String clinicId){
        List<ExamApply> examApplyList = examApplyService.getByClinicId(clinicId);
        Message mes = new Message();
        mes.setData(examApplyList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 医生删除检查申请
     * @param id
     * @Author ct
     * @return
     */
    @RequestMapping(value = {"V100/deleteExam"})
    public String deleteExamV100(@RequestParam(required = true) String id){
        Message mes = new Message();
        int i = examApplyService.deleteExam(id);
        mes.setData(i);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 保存检查申请
     * @return
     */
    @RequestMapping(value = {"V100/saveExam"})
    public String saveExam(String examApply, String clincItemIds,String locactions) {
        JSONArray jsonArray = JSONArray.parseArray(examApply);
        JSONObject jsonJ = jsonArray.getJSONObject(0);
        String ss = jsonJ.toString();
        ExamApply examApply1 = JSON.parseObject(ss, ExamApply.class);
        Message mes = new Message();
        int result = examApplyService.saveExamApplyData(examApply1,clincItemIds,locactions);
        if (result > 0) {
            mes.setData("保存检查申请成功");

        } else {
            mes.setData("保存检查申请失败");
        }
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 删除检查申请
     * @param examApply
     * @return
     * @Author jict
     */
    @RequestMapping(value = "delete")
    public StringData delete(ExamApply examApply) {
        StringData data = new StringData();
        ExamApply examApply1 = examApplyService.get(examApply);
        if ("1".equals(examApply1.getChargeFlag())) {
            data.setCode("chargeFlag");
            data.setData("检查已收费，请刷新页面！");
            return data;
        }
        int i = examApplyService.delete(examApply);
        if (i > 0) {
            data.setCode("success");
            data.setData("删除检查申请成功");
        } else {
            data.setCode("error");
            data.setData("删除检查申请失败");
        }
        return data;
    }

    /**
     * 获取检查大类别
     * @Author jict
     */
    @RequestMapping(value = {"V100/examType"})
    public String examType(String orgId, HttpServletRequest request, HttpServletResponse response) {
        ExamType examType = new ExamType();
        examType.setLevel("1");
        examType.setOrgId(orgId);
        List<ExamType> examTypeList = examTypeService.findList(examType);
        Message mes = new Message();
        mes.setData(examTypeList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 根据上级检查项目查询下级检查项目
     *
     * @param
     * @param request
     * @param response
     * @return
     * @Author jict
     */
    @RequestMapping(value = {"V100/queryByPId"})
    public String queryByPId(String id, HttpServletRequest request, HttpServletResponse response) {
        Message mes = new Message();
        List<ExamType> list = examTypeService.findByPId(id);
        mes.setData(list);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 京顺医院根据上级检查项目查询下级检查项目
     *
     * @param
     * @param request
     * @param response
     * @return
     * @Author jict
     */
    @RequestMapping(value = {"V200/queryByPId"})
    public String queryByPIds(String id, HttpServletRequest request, HttpServletResponse response) {
        Message mes = new Message();
        List<ExamVsClinic> list = examTypeService.getByParentId(id);
        mes.setData(list);
        return SecurityUtil.encryptMsgData(mes);
    }

  /*  *//**
     * 京顺医院保存检查申请
     * @return
     *//*
    @RequestMapping(value = {"V200/saveExam"})
    public String saveExams(String examApply, String clincItemIds,String locactions) {
        JSONArray jsonArray = JSONArray.parseArray(examApply);
        JSONObject jsonJ = jsonArray.getJSONObject(0);
        String ss = jsonJ.toString();
        ExamApply examApply1 = JSON.parseObject(ss, ExamApply.class);
        Message mes = new Message();
        examApply1 = examApplyService.saveexamApply(examApply1,clincItemIds,locactions);
        if (examApply1 != null) {
            mes.setData("保存检查申请成功");
        } else {
            mes.setData("保存检查申请失败");
        }
        return SecurityUtil.encryptMsgData(mes);
    }
*/
}